package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ArticleDAO;
import entity.Article;

/**
 * Servlet implementation class Ta_Home
 */
@WebServlet("/Ta_Home")
public class Ta_Home extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ta_Home() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int user_id = 0;
		try {
			user_id = Integer.parseInt(request.getParameter("id"));
		} catch (NumberFormatException e) {
			// 如果浏览器没有传递start值
		}
		if(user_id==0)
		{
		try {
			user_id = Integer.parseInt(request.getAttribute("id").toString());
		} catch (NumberFormatException e) {
			// 如果浏览器没有传递start值
		}
		}
		
		List<Article> articles = ArticleDAO.getInstance().list_user_top(user_id);
		List<Article> articles2 = ArticleDAO.getInstance().list_use(user_id);
		for (Article a : articles2) {
			articles.add(a);
		}
		
		request.setAttribute("aim_user_id", user_id);
		request.setAttribute("articles", articles);
		request.getRequestDispatcher("tahome.jsp").forward(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
